import { Query } from "./queryevent"
export const RoadCondition = (map) => {
  const flag = ref(false)
  // const source = ref()
  var flowsource
  var lins = []
  // 查看路况
  function handleRoad() {
    flag.value = true
    var service = {
      name: "guanggu",
      layerId: 1,
    }
    Query.queryByLayer({
      service,
      callback: (res) => {
        let coordinates = []
        let roadId = []
        let roadInfo = []
        res.forEach((item) => {
          roadInfo.push(item.values_.values_.车流量)
          roadId.push(item.getId())
          const a = item.getGeometry().flatCoordinates
          const b = []
          for (let i = 0; i < a.length; i += 2) {
            b.push([a[i], a[i + 1]])
          }
          coordinates.push(b)
        })
        for (let i = 0; i < res.length; i++) {
          var flow = roadInfo[i]
          //创建一个线
          var line = new ol.Feature({
            geometry: new ol.geom.LineString(coordinates[i]),
          })
          let color
          if (flow < 1000) {
            color = "rgb(104, 225, 134)"
          } else if (flow >= 1000 && flow < 1500) {
            color = "rgb(233, 244, 114)"
          } else if (flow >= 1500) {
            color = "rgb(199, 29, 3)"
          }
          //设置线的样式
          line.setStyle(
            new ol.style.Style({
              //边线颜色
              stroke: new ol.style.Stroke({
                color: color,
                lineJoin: "square",
                //lineJoin: "miter",//round或miter
                width: 8,
              }),
            })
          )
          lins.push(line)
        }
        if (flowsource == null) {
          flowsource = new ol.source.Vector({
            wrapX: false,
          })
          //创建一个图层
          var flowlayer = new ol.layer.Vector({
            source: flowsource,
          })
          //将绘制层添加到地图容器中
          map.value.addLayer(flowlayer)
        }
        flowsource.addFeatures(lins)
      },
    })
  }
  // 关闭路况
  function handleClose() {
    flag.value = false
    flowsource.clear()
  }

  return {
    handleClose,
    handleRoad,
    flag,
  }
}
